﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Srs.Sample.UI.CustomerService;
using System.Reflection;

namespace Srs.Sample.UI.WebParts
{
    [Guid("3B78B41A-9ABF-46b6-93B4-790DE56D186C")]
    public class SrsSampleGridView : Microsoft.SharePoint.WebPartPages.WebPart
    {

        IEnumerable<Customer> _customers;
        UpdatePanel _updatePanel;
        UpdateProgress _updProgress;
        bool _error;

        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();

                    //UpdatePanel stuff
                    this.EnsurePanelFix();

                    //Get Script Manager for the control and page
                    this.Controls.Add(this.GetScriptManager());

                    //create the update panel
                    _updatePanel = new UpdatePanel();
                    _updatePanel.ID = "updatePanel";
                    _updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
                    _updatePanel.ChildrenAsTriggers = true;


                    //the view
                    SPGridView spGrid;
                    spGrid = this.BuildGrid(typeof(Customer));
                    spGrid.PageSize = 5;
                    spGrid.AllowPaging = true;
                    spGrid.PagerSettings.Mode = PagerButtons.NumericFirstLast;
                    spGrid.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e)
                    {
                        spGrid.DataSource = GetCustomerList();
                        spGrid.PageIndex = e.NewPageIndex;
                        spGrid.DataBind();
                    };

                    //Add grid to the updatepanel
                    _updatePanel.ContentTemplateContainer.Controls.Add(spGrid);

                    _updatePanel.AddLineBreak();

                    //Add Update panel to the control collection 2nd step
                    this.Controls.Add(_updatePanel);


                    //Needs to happen AFTER adding to all Controls Collections...
                    spGrid.PagerTemplate = null;


                    //Get the data and Bind...
                    spGrid.DataSource = GetCustomerList();
                    spGrid.DataBind();
                }
                catch (Exception ex)
                {
                    _updatePanel.HandleException(ex, ref _error, false);
                }
            }


        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            EnsureChildControls();

            _updProgress = new UpdateProgress();
            _updProgress.AssociatedUpdatePanelID = "updatePanel";
            _updProgress.ProgressTemplate = new ProgressTemplate();

            this.Controls.Add(_updProgress);
        }



        IEnumerable<Customer> GetCustomerList()
        {
            CustomerService.CustomerServiceClient svc = new CustomerServiceClient();
            try
            {
                if (_customers == null)
                {
                    _customers = svc.GetCustomers().AsEnumerable();

                    //Testing
                    System.Threading.Thread.Sleep(2000);
                }
                return _customers;
            }
            catch (Exception ex)
            {
                _updatePanel.HandleException(ex, ref _error, false);
                return null;
            }
            finally
            {
                if ( svc != null ) svc.Dispose();
            }
       }

    }
}
